package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.MedicalVo;
import com.ruoyi.system.domain.vo.VerifyVo;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 药品规则Controller
 * 
 * @author wmb
 * @date 2020-07-28
 */
@RestController
@RequestMapping("/rule")
public class DrugRuleController extends BaseController
{
    @Autowired
    private IDrugRuleService drugRuleService;

    @Autowired
    private IDrugService drugService;

    @Autowired
    private IDrugVerifyService drugVerifyService;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private ICaseHistoryService caseHistoryService;
    /**
     * 查询药品规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrugRule drugRule)
    {
        startPage();
        List<DrugRule> list = drugRuleService.selectDrugRuleList(drugRule);
        return getDataTable(list);
    }

    /**
     * 导出药品规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:rule:export')")
    @Log(title = "药品规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrugRule drugRule) throws IOException
    {
        List<DrugRule> list = drugRuleService.selectDrugRuleList(drugRule);
        ExcelUtil<DrugRule> util = new ExcelUtil<DrugRule>(DrugRule.class);
        util.exportExcel(response, list, "rule");
    }

    /**
     * 获取药品规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:rule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(drugRuleService.selectDrugRuleById(id));
    }

    /**
     * 新增药品规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:add')")
    @Log(title = "药品规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrugRule drugRule) {
        //后期前端改后去掉
        Drug drug = drugService.selectDrugByName(drugRule.getDrugName());
        if(drug!=null){
            drugRule.setDrugId(drug.getId());
        }
        int count = drugRuleService.insertDrugRule(drugRule);
        if(count > 0)
            return AjaxResult.success(drugRule.getId());
        return AjaxResult.error();
    }

    /**
     * 修改药品规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:edit')")
    @Log(title = "药品规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrugRule drugRule)
    {
        return toAjax(drugRuleService.updateDrugRule(drugRule));
    }

    /**
     * 删除药品规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:remove')")
    @Log(title = "药品规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drugRuleService.deleteDrugRuleByIds(ids));
    }

    @PostMapping("/verify")
    public AjaxResult verify(@RequestBody VerifyVo verify) {



        Boolean expression = (Boolean) drugRuleService.verify(verify).get("result");

        return AjaxResult.success(expression);
    }



    @PostMapping("/addMedical")
    public AjaxResult addMedical(@RequestBody MedicalVo medical) {

        Drug drug = drugService.selectDrugByName(medical.getDrugName());
        if(drug == null){
            return AjaxResult.error("药品不存在");
        }

        CaseHistory history = new CaseHistory();
        history.setTreatmentAfter(medical.getComplain());
        history.setOutDiagnosis(medical.getComplain());

        caseHistoryService.insertCaseHistory(history);

        DrugVerify verify = new DrugVerify();
        verify.setAdmissionId(history.getId());
        verify.setCode(medical.getCode());
        verify.setDrugId(drug.getId());
        verify.setDrugName(medical.getDrugName());

        drugVerifyService.insertDrugVerify(verify);

        return AjaxResult.success("录入成功");
    }





}
